package com.definesys.smecrailtransit.widget;

import android.content.Context;
import android.graphics.Color;
import com.definesys.smecrailtransit.module.lineCharts.beans.MonStations;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.LargeValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by xupeizuo on 2018/3/5.
 */

public class ChartsController {

    private String [] colorArr=new String[]{"#60b4ae","#e5857c"};
    private String [] barColor=new String[]{"#72cdd0","#c471d9","#df9a65","#8de19c","#6e6fe5"};

    private PieChart mPieChart;
    private Context mContext;
    private BarChart mBarChart;

    public ChartsController(PieChart mPieChart, Context mContext,String[] coArr) {
        this.mPieChart = mPieChart;
        this.mContext = mContext;
        this.colorArr=coArr;
    }

    public ChartsController(BarChart barChart, Context mContext) {
        this.mBarChart = barChart;
        this.mContext = mContext;
    }


    public void initPie(){
        // 显示百分比
        mPieChart.setUsePercentValues(false);
        // 设置偏移量
        mPieChart.setExtraOffsets(5, 10, 5, 5);
        // 设置滑动减速摩擦系数
        mPieChart.setDragDecelerationFrictionCoef(0.95f);
        mPieChart.getDescription().setEnabled(false);
        /*
            设置饼图中心是否是空心的
            true 中间是空心的，环形图
            false 中间是实心的 饼图
         */
        mPieChart.setDrawHoleEnabled(true);
        /*
            设置中间空心圆孔的颜色是否透明
            true 透明的
            false 非透明的
         */
//        mPieChart.setHoleColorTransparent(true);
        // 设置环形图和中间空心圆之间的圆环的颜色
        mPieChart.setTransparentCircleColor(Color.WHITE);
        // 设置环形图和中间空心圆之间的圆环的透明度
        mPieChart.setTransparentCircleAlpha(110);

        // 设置圆孔半径
        mPieChart.setHoleRadius(58f);
        // 设置空心圆的半径
        mPieChart.setTransparentCircleRadius(61f);
        // 设置是否显示中间的文字
        mPieChart.setDrawCenterText(false);

        mPieChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mPieChart.setRotationEnabled(true);
        mPieChart.setHighlightPerTapEnabled(false);

        mPieChart.setDrawEntryLabels(false);   // 同上,默认true，记住颜色和环不要一样，否则会显示不出来
        mPieChart.setUsePercentValues(true);    // 表内数据用百分比替代，而不是原先的值。并且ValueFormatter中提供的值也是该百分比的。默认false
        mPieChart.setCenterTextRadiusPercent(100f); // 中心文本边界框矩形半径比例，默认是100%.
        mPieChart.setMaxAngle(360);
        mPieChart.getLegend().setEnabled(false);
//        Legend mLegend = mPieChart.getLegend();  //设置比例图
//        mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);  //最右边显示
////      mLegend.setForm(LegendForm.LINE);  //设置比例图的形状，默认是方形
//        mLegend.setXEntrySpace(7f);
//        mLegend.setYEntrySpace(5f);


    }


    public ChartsController CenterText(String centerText){
        // 描述信息
        mPieChart.setCenterText(centerText);
        return this;
    }

    public void setPieData(ArrayList<Integer> data){

        List<Integer> colors = new ArrayList<>();
        colors.add(Color.parseColor(colorArr[0]));
        colors.add(Color.parseColor(colorArr[1]));

        if(CommonUtils.notEmpty(data)){
            List<PieEntry> pieEntries = new ArrayList<>();
            for(int i=0;i<data.size();i++){
                pieEntries.add(i,new PieEntry(data.get(i),""));
            }
            PieDataSet pieDataSet=new PieDataSet(pieEntries,"");
            pieDataSet.setColors(colors);
            pieDataSet.setValueTextColors(colors);
            pieDataSet.setSliceSpace(1);
            PieData pieData = new PieData(pieDataSet);
            mPieChart.setData(pieData);
        }
    }


    /**
     * 开始加载
     */
    public void startLoaf(){
        // 设置动画
        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);

        // 设置显示的比例
        Legend l = mPieChart.getLegend();
        l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);
    }


    /**
     * 初始化柱状图信息
     * @param mChart
     * @param monStationses
     */
    public void initBar(BarChart mChart, ArrayList<MonStations> monStationses){

        try {
            mChart.setPinchZoom(false);
            mChart.setDrawBarShadow(false);
            mChart.setDrawGridBackground(false);
            mChart.getLegend().setEnabled(false);
            mChart.getDescription().setEnabled(false);

            XAxis xAxis = mChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setDrawGridLines(false);
            xAxis.setDrawAxisLine(true);
            xAxis.setTextColor(Color.BLACK);
            xAxis.setTextSize(8f);
            xAxis.setLabelCount(5);
            xAxis.setGranularity(1f);
            xAxis.setXOffset(0);


            YAxis leftAxis = mChart.getAxisLeft();
            leftAxis.setDrawTopYLabelEntry(false);
            leftAxis.setValueFormatter(new LargeValueFormatter());
            leftAxis.setDrawGridLines(false);
            leftAxis.setSpaceTop(35f);

            mChart.getAxisRight().setEnabled(false);

            List<String> xString=new ArrayList<>();
            List<IBarDataSet> arrayList=new ArrayList<>();
            if(CommonUtils.notEmpty(monStationses)){
                for(int i=0;i<monStationses.size();i++){
                    MonStations monStation=monStationses.get(i);
                    ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
                    yVals.add(new BarEntry(i, monStation.getStationFaultCount()));
                    xString.add(monStation.getStationName());
                    BarDataSet set=new BarDataSet(yVals,monStation.getStationName());
                    set.setColor(Color.parseColor(barColor[i]));
                    arrayList.add(set);
                }
            }

            xAxis.setValueFormatter(new CustomYValueFormatter(xString));

            BarData data = new BarData(arrayList);
            data.setValueTextSize(1f);
            data.setDrawValues(true);
            data.setBarWidth(0.5f);
            data.setValueFormatter(new LargeValueFormatter());
            mChart.animateXY(1000, 2000);
            mChart.setData(data);
        }catch (Exception e){
            e.printStackTrace();
        }
  }

    private class CustomYValueFormatter implements IAxisValueFormatter{

        private List<String> labels;

        public CustomYValueFormatter(List<String> labels) {
            this.labels = labels;
        }

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            if(value<0){
                return "";
            }
            return labels.get((int) value % labels.size());
        }
    }
}
